class User < ActiveRecord::Base
  has_paper_trail :version => :paper_trail_version, :versions => :paper_trail_versions
  rolify
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :omniauthable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :orders

  scope :desc, order("created_at DESC")

  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :nickname, :uid, :provider
  def self.find_by_connect_oauth(auth, signed_in_resource=nil)
    user = User.where(:provider => auth.provider, :uid => auth.uid.to_s).first
    
  end

  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.connect_data"] and session["devise.connect_data"]["info"]
        user.nickname = data['info']['nickname'] if user.nickname.blank?
        user.provider =  data['provider']
        user.uid = data['uid']
        user.avatar = data['image']
      end
      puts data.inspect
    end
  end

end
